package com.jpress.beans;

import java.util.Calendar;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.context.RequestContext;

import com.jpress.controller.UsuarioController;
import com.jpress.exceptions.CrudException;
import com.jpress.model.Blog;
import com.jpress.model.Usuario;
import com.jpress.utils.FacesUtils;

@ManagedBean(name="UsuarioBlogBean")
@ViewScoped
public class UsuarioBlogBean {
	
	private Usuario usuario;
	private Blog blog;
	
	@ManagedProperty(value="#{SessaoBean}")
	private SessaoBean sessaoBean;
	
	private UsuarioController usuarioController;
	
	@PostConstruct
	public void init(){
		usuario = sessaoBean.getUsuarioAutenticado();
		usuarioController = UsuarioController.getInstance();
		if(usuario.getBlog() != null)
			blog = usuario.getBlog();
	}
	
	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public Blog getBlog() {
		return blog;
	}

	public void setBlog(Blog blog) {
		this.blog = blog;
	}

	public SessaoBean getSessaoBean() {
		return sessaoBean;
	}

	public void setSessaoBean(SessaoBean sessaoBean) {
		this.sessaoBean = sessaoBean;
	}
	
	public void salvarBlog(){
		
		try {
			this.blog.setDono(this.usuario);
			this.blog.setDataCriacao(Calendar.getInstance().getTime());
			this.usuario.setBlog(this.blog);
			this.usuarioController.atualizar(this.usuario);
			FacesUtils.addSucessMessage("indexGrowl", FacesUtils.getParamMessageBundle("crud.sucessoMensagens.insercao", FacesUtils.getMessageBundle("blog")));
			RequestContext.getCurrentInstance().update("indexGrowl");
			RequestContext.getCurrentInstance().update("tabView");
			RequestContext.getCurrentInstance().execute("criarBlogDlg.hide();");
		} catch(CrudException ce){
			FacesUtils.addErrorMessage("indexGrowl", ce.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}			
	}
	
	public void atualizarBlog(){
		try {
			this.usuarioController.atualizar(this.usuario);
			FacesUtils.addSucessMessage("indexGrowl", FacesUtils.getParamMessageBundle("crud.sucessoMensagens.atualizacao", FacesUtils.getMessageBundle("blog")));
			RequestContext.getCurrentInstance().update("indexGrowl");
			RequestContext.getCurrentInstance().update("tabView:edicaoBlogForm");
		} catch(CrudException ce){
			FacesUtils.addErrorMessage("indexGrowl", ce.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void prepararCadastroBlog(){
		this.blog = new Blog();
		RequestContext.getCurrentInstance().reset("tabView:cadastroBlogForm");
		RequestContext.getCurrentInstance().execute("criarBlogDlg.show();");
	}
	
	public void cancelarCadastroBlog(){
		RequestContext.getCurrentInstance().reset("tabView:cadastroBlogForm");
		RequestContext.getCurrentInstance().execute("criarBlogDlg.hide();");
	}	
}
